Imports System.Data

Partial Class WebForms_Rider
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = parentpage
        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cf As New CIMSfunc

        cf.setNumericFormat(Benefits_txt)
        cf.setNumericFormat(Premiums_txt)

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim dao As New DAOMain.CommonDAO
            Dim lfunc As New PolyFunc.ASPFunc

            Dim dt As DataTable = dao.ExecQuery("Select prem_rider_cd, prem_rider_desc From prem_rider_mstr Order by prem_rider_desc")

            lfunc.BindDropDown(Rider_cmb, dt, "prem_rider_desc", "prem_rider_cd")

            Dim cmbi As New ListItem

            cmbi.Value = "BASIC               "
            cmbi.Text = "BASIC"

            Rider_cmb.Items.Insert(1, cmbi)
            cmbi = Nothing

            Call InitVars()
            policy_td.InnerText = ViewState("PolicyNo")
            provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()
            Call LoadRider()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
                StartDate_txt.Enabled = False
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If
    End Sub

    Private Sub LoadRider()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_PREM_RIDER_DTL_DAO
        Dim vo As New DAOMain.POLICY_PREM_RIDER_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.PREM_RIDER_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Rider_cmb.SelectedValue = .Item("PREM_RIDER_CD")
                Benefits_txt.Text = cFunc.loadFormat(.Item("BENEFITS"))
                StartDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_PREM_RIDER_START_DT"))
                EndDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_PREM_RIDER_END_DT"))
                Premiums_txt.Text = cFunc.loadFormat(.Item("PREMIUM_AMT"))
                Remarks_txt.Text = .Item("REMARKS")
            End With
        End If

        setFieldDisplay()

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        If Trim(EndDate_txt.Text) <> "" Then

            Dim sDate As Date = cFunc.ParseSetDate(StartDate_txt.Text)
            Dim eDate As Date = cFunc.ParseSetDate(EndDate_txt.Text)


            If eDate < sDate Then
                polyFunc.MessageAlert(Me.ParentPage, "Invalid Date Range!")
                Exit Sub
            End If
        End If

        Dim dao As New DAOMain.POLICY_PREM_RIDER_DTL_DAO
        Dim vo As New DAOMain.POLICY_PREM_RIDER_DTL_VO
        Dim voCheck As New DAOMain.POLICY_PREM_RIDER_DTL_VO
        Dim daoCHeck As New DAOMain.POLICY_PREM_RIDER_DTL_DAO

        Dim dt As DataTable

        With voCheck
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .PREM_RIDER_CD = Rider_cmb.SelectedValue.ToString

            dt = daoCHeck.Search(voCheck)
        End With

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")


            If StartDate_txt.Visible = True Then
                .POLICY_PREM_RIDER_START_DT = cFunc.ParseSetDate(StartDate_txt.Text)
                .POLICY_PREM_RIDER_END_DT = cFunc.ParseSetDate(EndDate_txt.Text)

            Else
                Dim daoPolicy As New DAOMain.POLICY_TRANS_DAO
                Dim voPolicy As New DAOMain.POLICY_TRANS_VO

                voPolicy.POLICY_SEQ_CD = ViewState("Seq_CD")
                Dim dtPolicy As DataTable = daoPolicy.Search(voPolicy)

                If dtPolicy.Rows.Count > 0 Then .POLICY_PREM_RIDER_START_DT = dtPolicy.Rows(0)("POLICY_EFF_DT")
                '.BENEFITS = 0
                .POLICY_PREM_RIDER_END_DT = cFunc.ParseSetDate("")

                daoPolicy = Nothing
                voPolicy = Nothing
            End If

            .BENEFITS = Val(cFunc.saveFormat(Benefits_txt.Text))
            .PREMIUM_AMT = Val(cFunc.saveFormat(Premiums_txt.Text))
            .REMARKS = Remarks_txt.Text

            Select Case dt.Rows.Count
                Case 0
                    If IsNothing(ViewState("CID")) Then
                        .PREM_RIDER_CD = Rider_cmb.SelectedValue
                        .POLICY_PREM_RIDER_CREA_BY = Session("UserName")
                        .POLICY_PREM_RIDER_CREA_DT = Now

                        If dao.Create_Record(vo) Then
                            InitSessions()
                            Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                        Else
                            polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                        End If
                    End If
                Case Else
                    If Trim(ViewState("CID")) = Trim(Rider_cmb.SelectedValue) Then
                        .PREM_RIDER_CD = Rider_cmb.SelectedValue
                        .POLICY_PREM_RIDER_LAST_UPD_BY = Session("UserName")
                        .POLICY_PREM_RIDER_LAST_UPD_DT = Now

                        If dao.Update(vo) Then
                            InitSessions()
                            Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                        Else
                            polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                        End If
                    Else
                        polyFunc.MessageAlert(ParentPage, "Rider already exists!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Private Sub UpdatePolicySum()
        Dim dao As New DAOMain.POLICY_TRANS_DAO
        Dim vo As New DAOMain.POLICY_TRANS_VO

        dao = Nothing
        vo = Nothing
    End Sub

    Private Function GetTotalPremium() As Double
        Dim dReturn As Double = 0

    End Function

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
    End Sub

    Protected Sub Rider_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rider_cmb.SelectedIndexChanged
        setFieldDisplay()
    End Sub

    Private Sub setFieldDisplay()
        If Trim(Rider_cmb.SelectedValue) = "BASIC" Then
            'tdBenefits.InnerText = ""
            tdEndDate.InnerText = ""
            tdStartDate.InnerText = ""

            'Benefits_txt.Visible = False
            StartDate_txt.Visible = False
            EndDate_txt.Visible = False

            lblEndDate.Visible = False
            lblStartDate.Visible = False
        Else
            'tdBenefits.InnerText = " Benefits:"
            tdEndDate.InnerText = " End Date:"
            tdStartDate.InnerText = " Start Date:"

            'Benefits_txt.Visible = True
            StartDate_txt.Visible = True
            EndDate_txt.Visible = True

            lblEndDate.Visible = True
            lblStartDate.Visible = True
        End If
    End Sub


End Class
